package com.example.hellonotes.activity;

import com.example.hellonotes.BaseActivity;
import com.example.hellonotes.BasePageActivity;
import com.example.hellonotes.R;
import com.example.hellonotes.bmob.UserProxy;
import com.example.hellonotes.bmob.UserProxy.ILoginListener;
import com.example.hellonotes.bmob.UserProxy.ISignUpListener;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

/**
 * 用于用户登录的类
 * @author Administrator
 *
 */

public class loginActivity extends BasePageActivity implements OnClickListener,ISignUpListener,ILoginListener{

	TextView loginTitle;
	TextView registerTitle;
	TextView resetPassword;
	
	EditText userNameInput;
	EditText userPasswordInput;
	EditText userEmailInput;
	
	String userName,userPassword,userEmail;
	
	Button registerButton,weiboregisterButton;
	UserProxy userProxy;	
	
	
	private enum UserOperation{
		LOGIN,REGISTER,RESET_PASSWORD
	}
	
	UserOperation operation = UserOperation.LOGIN;

	
	
	protected void setLayoutView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.login_activity);
	}

	
	protected void findViews() {
		// TODO Auto-generated method stub
		
		loginTitle = (TextView)findViewById(R.id.login_menu);
		registerTitle = (TextView)findViewById(R.id.register_menu);
		resetPassword = (TextView)findViewById(R.id.reset_password_menu);
		
		userNameInput = (EditText)findViewById(R.id.user_name_input);
		userPasswordInput = (EditText)findViewById(R.id.user_password_input);
		userEmailInput = (EditText)findViewById(R.id.user_email_input);
		
		registerButton = (Button)findViewById(R.id.register);
		weiboregisterButton = (Button)findViewById(R.id.weiboregister);
		
/*		userName = userNameInput.getText().toString().trim();
		userPassword = userPasswordInput.getText().toString().trim();
		userEmail = userEmailInput.getText().toString().trim();*/
	
	}
	

	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.register:
			if(operation == UserOperation.LOGIN){
				if(TextUtils.isEmpty(userNameInput.getText())){
					return;
				}
				if(TextUtils.isEmpty(userPasswordInput.getText())){
					return;
				}
				
				
				userProxy.setOnLoginListener(this);
				userProxy.login(userNameInput.getText().toString().trim(), userPasswordInput.getText().toString().trim());
				
			}else if(operation == UserOperation.REGISTER){
				if(TextUtils.isEmpty(userNameInput.getText())){
					Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
					return;
				}
				if(TextUtils.isEmpty(userPasswordInput.getText())){
					Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
					return;
				}
				if(TextUtils.isEmpty(userEmailInput.getText())){
					Toast.makeText(this, "请输入邮箱地址", Toast.LENGTH_SHORT).show();
					return;
				}
				//if(!StringUtils.isValidEmail(userEmailInput.getText())){
					//return;
				//}
				userProxy.setOnSignUpListener(this);
				userProxy.singup(userNameInput.getText().toString().trim(), userPasswordInput.getText().toString().trim(), userEmailInput.getText().toString().trim());
			}else{
				if(TextUtils.isEmpty(userEmailInput.getText())){
					return;
				}
				//if(!StringUtils.isValidEmail(userEmailInput.getText())){
					//return;
				//}
				
			}
			break;
		case R.id.login_menu:
			operation = UserOperation.LOGIN;
			updateLayout(operation);
			break;
		case R.id.register_menu:
			operation = UserOperation.REGISTER;
			updateLayout(operation);
			break;
		case R.id.reset_password_menu:
			operation = UserOperation.RESET_PASSWORD;
			updateLayout(operation);
			break;
		case R.id.weiboregister:
			intent2Activity(weibologinActivity.class);
			break;
		}
	}
	
	private void updateLayout(UserOperation op){
		if(op == UserOperation.LOGIN){
			loginTitle.setTextColor(Color.parseColor("#D95555"));
			loginTitle.setBackgroundResource(R.drawable.bg_login_tab);
			loginTitle.setPadding(16, 16, 16, 16);
			loginTitle.setGravity(Gravity.CENTER);
			
			
			registerTitle.setTextColor(Color.parseColor("#888888"));
			registerTitle.setBackgroundDrawable(null);
			registerTitle.setPadding(16, 16, 16, 16);
			registerTitle.setGravity(Gravity.CENTER);
			
			resetPassword.setTextColor(Color.parseColor("#888888"));
			resetPassword.setBackgroundDrawable(null);
			resetPassword.setPadding(16, 16, 16, 16);
			resetPassword.setGravity(Gravity.CENTER);
			
			userNameInput.setVisibility(View.VISIBLE);
			userPasswordInput.setVisibility(View.VISIBLE);
			userEmailInput.setVisibility(View.GONE);
			registerButton.setText("登录");
		}else if(op == UserOperation.REGISTER){
			loginTitle.setTextColor(Color.parseColor("#888888"));
			loginTitle.setBackgroundDrawable(null);
			loginTitle.setPadding(16, 16, 16, 16);
			loginTitle.setGravity(Gravity.CENTER);
			
			registerTitle.setTextColor(Color.parseColor("#D95555"));
			registerTitle.setBackgroundResource(R.drawable.bg_login_tab);
			registerTitle.setPadding(16, 16, 16, 16);
			registerTitle.setGravity(Gravity.CENTER);
			
			resetPassword.setTextColor(Color.parseColor("#888888"));
			resetPassword.setBackgroundDrawable(null);
			resetPassword.setPadding(16, 16, 16, 16);
			resetPassword.setGravity(Gravity.CENTER);
			
			userNameInput.setVisibility(View.VISIBLE);
			userPasswordInput.setVisibility(View.VISIBLE);
			userEmailInput.setVisibility(View.VISIBLE);
			registerButton.setText("注册");
		}else{
			loginTitle.setTextColor(Color.parseColor("#888888"));
			loginTitle.setBackgroundDrawable(null);
			loginTitle.setPadding(16, 16, 16, 16);
			loginTitle.setGravity(Gravity.CENTER);
			
			registerTitle.setTextColor(Color.parseColor("#888888"));
			registerTitle.setBackgroundDrawable(null);
			registerTitle.setPadding(16, 16, 16, 16);
			registerTitle.setGravity(Gravity.CENTER);
			
			resetPassword.setTextColor(Color.parseColor("#D95555"));
			resetPassword.setBackgroundResource(R.drawable.bg_login_tab);
			resetPassword.setPadding(16, 16, 16, 16);
			resetPassword.setGravity(Gravity.CENTER);
			
			
			userNameInput.setVisibility(View.GONE);
			userPasswordInput.setVisibility(View.GONE);
			userEmailInput.setVisibility(View.VISIBLE);
			registerButton.setText("找回密码");
		}
	}


	@Override
	protected void setupViews(Bundle bundle) {
		// TODO 自动生成的方法存根
		updateLayout(operation);
		userProxy = new UserProxy(this);
		
	}

	@Override
	protected void setListener() {
		// TODO 自动生成的方法存根
		
		loginTitle.setOnClickListener(this);
		registerTitle.setOnClickListener(this);
		resetPassword.setOnClickListener(this);
		registerButton.setOnClickListener(this);
		weiboregisterButton.setOnClickListener(this);
	}

	@Override
	protected void fetchData() {
		// TODO 自动生成的方法存根
		
	}


	@Override
	public void onSignUpSuccess() {
		// TODO 自动生成的方法存根
		System.out.println("注册成功");
	}


	@Override
	public void onSignUpFailure(String msg) {
		// TODO 自动生成的方法存根
		System.out.println("注册失败"+msg);
	}


	@Override
	public void onLoginSuccess() {
		// TODO 自动生成的方法存根
		System.out.println("bmob登录成功");
		intent2Activity(MainActivity.class);
	}


	@Override
	public void onLoginFailure(String msg) {
		// TODO 自动生成的方法存根
		System.out.println("bmob登录失败");
	}
	
}
